package com.miplus.ccrm.core.service.cust;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.core.controller.vo.LabelResVo;
import com.miplus.ccrm.core.controller.vo.cust.AddrEditVo;
import com.miplus.ccrm.core.controller.vo.cust.AddrReqVo;
import com.miplus.ccrm.core.controller.vo.cust.AddrResVo;
import com.miplus.ccrm.core.controller.vo.cust.ContResVo;
import com.miplus.ccrm.core.pojo.ConAddress;
import com.miplus.ccrm.core.mapper.ConAddressMapper;
import com.miplus.ccrm.core.pojo.ConContact;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author pc
* @description 针对表【con_address(客户地址表)】的数据库操作Service实现
* @createDate 2024-03-20 11:03:18
*/
@Service
public class ConAddressServiceImpl extends MPJBaseServiceImpl<ConAddressMapper, ConAddress>
    implements ConAddressService{

    @Override
    public IPage<AddrResVo> addressList(AddrReqVo paras)
    {
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                AddrResVo.class,
                new MPJLambdaWrapper<ConAddress>()
                        .selectAs("(select dsc from sys_names n where n.tid='ADDRTYP' and n.id=t.addresstype)",AddrResVo::getAddresstypedsc)
                        .selectAs("(select n.dsc from sys_names n where n.tid='DATA.ORIGIN' and n.id=t.origin)",AddrResVo::getOrigindsc)
                        .selectAll(ConAddress.class)
                        .eq("customernumber",paras.getCustomernumber())
        );
    }

    @Override
    public AddrResVo getAddressInfo(int addressid)
    {
        return selectJoinOne(
                AddrResVo.class,
                new MPJLambdaWrapper<ConAddress>()
                        .selectAs("(select dsc from sys_names n where n.tid='ADDRTYP' and n.id=t.addresstype)",AddrResVo::getAddresstypedsc)
                        .selectAll(ConAddress.class)
                        .eq("addressnumber",addressid)
        );
    }

    @Override
    public CmdResVo updateAddress(String $usrid, AddrEditVo paras)
    {
        ConAddress def=new ConAddress();
        BeanUtils.copyProperties(paras,def);
        def.setLastmodifiedby($usrid);
        def.setLastmodifieddate(new Date());
        int ret=baseMapper.updateById(def);
        return new CmdResVo(ret);
    }

    @Override
    public CmdResVo insertAddress(String $usrid,AddrEditVo paras)
    {
        ConAddress def=new ConAddress();
        BeanUtils.copyProperties(paras,def);
        if(JPubUtils.IsEmpty(paras.getOrigin())) {
            def.setOrigin("10"); //系统自建
        }
        def.setCreatedby($usrid);
        def.setCreateddate(new Date());
        def.setLastmodifiedby($usrid);
        def.setLastmodifieddate(new Date());
        int ret=baseMapper.insert(def);
        return new CmdResVo(ret,def.getAddressnumber().toString());
    }

    @Override
    public CmdResVo deleteAddress(int addressid)
    {
        int ret=baseMapper.deleteById(addressid);
        return new CmdResVo(ret);
    }
}




